import {
  getToken
} from '../../../utils/getToken'
import {
  formateDate
} from '../../../utils/date'
Page({
  data: {
    currentDate: Date.now(),
    isHouseLayerShow: false,
    isRepairLayerShow: false,
    isDateLayerShow: false,
    // 报修房屋列表
    repairHouseList: [],
    // 维修项目
    repairItemList: [],
    // 问题附件
    attachment: [],
    // 维修房屋id
    repairHouseId: '',
    // 维修房屋名称
    repairHouseName: '',
    // 维修项目id
    repairItemId: '',
    // 维修项目名称
    repairItemName: '',
    // 预约时间
    currentDate: Date.now(), // 当前时间
    appointment: '', // 预约上门时间
    // 手机号
    mobile: '',
    // 问题描述
    description: '',
    // 判断是编辑还是新增
    id: ''
  },
  onLoad(options) {
    // 报修房屋
    this.getrepairHouseList()
    // 维修项目
    this.getrepairItemList()
    // console.log('接受过来的数据', options);
    if (this.options.houseId) {
      this.getHouseRepairDetail(options.houseId)
    }
  },
  // 获取详情
  getHouseRepairDetail(id) {
    wx.request({
      url: `https://live-api.itheima.net/repair/${id}`,
      method: 'GET',
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      success: (res) => {
        if (res.data.code !== 10000) {
          wx.showToast({
            title: res.data.message,
          })
        }
        console.log('成功返回的数据', res);
        this.setData({
          // 如果是编辑，id就会有值
          id: res.data.data.id,
          repairHouseId: res.data.data.houseId,
          repairHouseName: res.data.data.houseInfo,
          repairItemId: res.data.data.repairItemId,
          repairItemName: res.data.data.repairItemName,
          mobile: res.data.data.mobile,
          description: res.data.data.description,
          appointment: res.data.data.appointment,
          attachment: res.data.data.attachment,
        })
      }
    })
  },
  // 报修房屋
  getrepairHouseList() {
    wx.request({
      url: 'https://live-api.itheima.net/house',
      method: 'GET',
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      success: (res) => {
        // console.log(res);
        if (res.data.code !== 10000) {
          return wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
        this.setData({
          repairHouseList: res.data.data
        })
      }
    })
  },
  handleRepairHouseSelect(e) {
    // console.log(e);
    this.setData({
      repairHouseId: e.detail.id,
      repairHouseName: e.detail.name,
    })
  },
  // 维修项目
  getrepairItemList() {
    wx.request({
      url: 'https://live-api.itheima.net/repairItem',
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      success: (res) => {
        // console.log(res);
        if (res.data.code !== 10000) {
          return wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
        this.setData({
          repairItemList: res.data.data
        })
      }
    })
  },
  handleRepairItemSelect(e) {
    this.setData({
      repairItemId: e.detail.id,
      repairItemName: e.detail.name
    })
  },
  // 预约日期
  handleDateCancel() {
    // 取消
    this.setData({
      isDateLayerShow: false
    })
  },
  handleDateConfirm(e) {
    // console.log('确定', e);
    this.setData({
      isDateLayerShow: false,
      appointment: formateDate(e.detail)
    })
  },
  // 问题附件
  handleImageAfterRead(e) {
    // console.log(e);
    wx.uploadFile({
      filePath: e.detail.file.url, // 上传图片的路径
      name: 'file',
      url: 'https://live-api.itheima.net/upload',
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      success: (res) => {
        const data = JSON.parse(res.data)
        console.log(data);
        if (this.data.code !== 10000) {
          wx.showToast({
            title: data.message,
          })
        }
        this.setData({
          attachment: [...this.data.attachment, data.data]
        })
      }
    })
  },
  openHouseLayer() {
    this.setData({
      isHouseLayerShow: true
    });
  },
  closeHouseLayer() {
    this.setData({
      isHouseLayerShow: false
    });
  },
  openRepairLayer() {
    this.setData({
      isRepairLayerShow: true
    });
  },
  closeRepairLayer() {
    this.setData({
      isRepairLayerShow: false,
    });
  },

  openDateLayer() {
    this.setData({
      isDateLayerShow: true
    });
  },
  closeDateLayer() {
    this.setData({
      isDateLayerShow: false
    });
  },
  // 提交报修
  handleRepairSubmit() {
    if (this.validateRepairData(this.data)) {
      wx.request({
        url: 'https://live-api.itheima.net/repair',
        method: 'POST',
        header: {
          Authorization: 'Bearer ' + getToken()
        },
        data: {
          id: this.data.id,
          houseId: this.data.repairHouseId,
          repairItemId: this.data.repairItemId,
          mobile: this.data.mobile,
          appointment: this.data.appointment,
          description: this.data.description,
          attachment: this.data.attachment
        },
        success: (res) => {
          if (res.data.code !== 10000) {
            wx.showToast({
              title: res.data.message,
              icon: "none",
            })
          }
          wx.redirectTo({
            url: '/repair_pkg/pages/list/index',
          });
        }
      })
    }
  },
  // 表单校验
  validateRepairData(data) {
    console.log('校验', data);
    // 报修房屋校验
    if (!data.repairHouseId.trim()) {
      wx.showToast({
        title: '请选择报修房屋',
        icon: 'none'
      })
      return false
    }
    // 保修项目校验
    if (!data.repairItemId.trim()) {
      wx.showToast({
        title: '请选择报修项目',
        icon: 'none'
      })
      return false
    }
    // 预约日期校验
    if (!data.appointment.trim()) {
      wx.showToast({
        title: '请选择预约日期',
        icon: 'none'
      })
      return false
    }
    // 问题描述校验
    if (!data.description.trim()) {
      wx.showToast({
        title: '请填写问题描述',
        icon: 'none'
      })
      return false
    }
    // 手机号码校验
    const mobileReg = /^[1][3-8][0-9]{9}$/
    if (!mobileReg.test(data.mobile.trim())) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      })
      return false
    }
    // 校验通过
    console.log('校验通过')
    return true
  },
});